package com.binest.personalcenter.controller;

import com.binest.common.core.controller.BaseController;
import com.binest.common.core.domain.AjaxResult;
import com.binest.common.core.redis.RedisCache;
import com.binest.register.service.IMailService;
import com.binest.util.GoogleAuthenticator;
import com.binest.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/personcenter")
public class PersonalCenterController extends BaseController {


    /**
     * 生成 Google 密钥，两种方式任选一种
     */
    @GetMapping("/getSecret")
    public AjaxResult getSecret() {
        return success(GoogleAuthenticator.getSecretKey());
    }

    /**
     * 获取code
     */
    @GetMapping("/getCode")
    public String getCode(String secretKey) {
        return GoogleAuthenticator.getCode(secretKey);
    }

    /**
     * 验证 code 是否正确
     */
    @GetMapping("/checkCode")
    public String checkCode(String secret, String code) {
        boolean b = GoogleAuthenticator.checkCode(secret, Long.parseLong(code), System.currentTimeMillis());
        if (b) {
            return "success";
        }
        return "error";
    }


}
